function createBlobUrl(base64AudioData: string) {
    let byteArrays = []
    let byteCharacters = atob(base64AudioData)
    for (let offset = 0; offset < byteCharacters.length; offset++) {
        let byteArray = byteCharacters.charCodeAt(offset)
        byteArrays.push(byteArray)
    }
    let blob = new Blob([new Uint8Array(byteArrays)], { type: 'audio/mp3' })
    // console.log(URL.createObjectURL(blob));

    return URL.createObjectURL(blob)
}
export async function generateAudio(test: string) {
    const token = import.meta.env.VITE_AUDID_ACCESS_TOKEN
    const appId = import.meta.env.VITE_AUDID_APP_ID
    const voiceName = 'zh_female_meilinvyou_moon_bigtts'
    const endpoint = '/tts/api/v1/tts'
    const headers = {
        Authorization: `Bearer;${token}`,
        'Content-Type': 'application/json',
    }
    const payload = {
      app: {
        appid: appId,
        token,
        cluster: "volcano_tts",
      },
      user: {
        uid: 'wn'
      },
      audio: {
        voice_type: voiceName,
        encoding: 'ogg_opus',
        speed_ratio: 1.0,
        emotion: 'happy',
      },
      request: {
        reqid: Math.random().toString(36).substring(7),
        text: 'test',
        text_type: 'plain',
        operation: 'query'
      }
    }
     const res = await fetch(endpoint, {
      method: 'POST',
      headers,
      body: JSON.stringify(payload),
    })
    const data = await res.json()
    if(!data.data){
      throw new Error('生成音频失败')

    }
    const audio = createBlobUrl(data.data.audio)
    return audio
}
